/*
 *  Copyright 2011 Alexey Andreev.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.xthl.core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.xthl.core.styling.CssItem;

/**
 *
 * @author Alexey Andreev
 */
public class PageResourceSetImpl implements PageResourceConsumer, PageResourceProvider {
    private static class CssKey {
        public final String media;
        public final String url;
        
        public CssKey(String media, String url) {
            this.media = media;
            this.url = url;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((media == null) ? 0 : media.hashCode());
            result = prime * result + ((url == null) ? 0 : url.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            CssKey other = (CssKey) obj;
            if (media == null) {
                if (other.media != null)
                    return false;
            } else if (!media.equals(other.media))
                return false;
            if (url == null) {
                if (other.url != null)
                    return false;
            } else if (!url.equals(other.url))
                return false;
            return true;
        }
    }
    
    private Set<CssKey> cssItems = new HashSet<CssKey>();
    private Set<String> scripts = new HashSet<String>();

    @Override
    public List<CssItem> getCss() {
        List<CssItem> items = new ArrayList<CssItem>();
        for (CssKey key : cssItems) {
            CssItem item = new CssItem();
            item.setMedia(key.media);
            item.setUrl(key.url);
            items.add(item);
        }
        Collections.sort(items, new Comparator<CssItem>() {
            @Override
            public int compare(CssItem o1, CssItem o2) {
                int r = o1.getUrl().compareTo(o2.getUrl());
                if (r != 0) {
                    return r;
                }
                return o1.getMedia().compareTo(o2.getMedia());
            }
        });
        return items;
    }

    @Override
    public List<String> getScripts() {
        List<String> result = new ArrayList<String>(scripts);
        Collections.sort(result);
        return result;
    }

    @Override
    public void addCss(CssItem item) {
        CssKey key = new CssKey(item.getMedia(), item.getUrl());
        cssItems.add(key);
    }

    @Override
    public void addScript(String url) {
        scripts.add(url);
    }
}
